package com.wsj.a.collapsingdemo.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;


import com.wsj.a.collapsingdemo.R;
import com.wsj.a.collapsingdemo.utils.Logger;


/**
 * 通用提示Dialog，支持定时消失或不消失
 *
 * @version 云和煦
 * */
public class GeneralDialog extends Dialog {
	private String TAG = "GeneralDialog";
	private View mView;
	private ProgressBar progressBar;
	private ImageView resultIcon;
	private TextView content;
	public final static int LOAD_HINT = 1;
	public final static int FAIL_HINT = 2;
	public final static int SUCCESS_HINT = 3;
	private static int type = LOAD_HINT;
	private boolean mCancelable = true;
	private static GeneralDialog generalDialog;
	private OnKeyListener onKeyListener = new OnKeyListener() {

		@Override
		public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
			// TODO 自动生成的方法存根
			if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
				if (isShowing()) {
					dismiss();
				}

			}
			return false;
		}

	};

	public synchronized static GeneralDialog getInstance(Context context) {
		if (generalDialog == null) {
			generalDialog = new GeneralDialog(context);
		}
		generalDialog.getWindow().setWindowAnimations(R.style.noWindowAnim);
		return generalDialog;
	}

	public GeneralDialog(Context context) {
		super(context, R.style.blackDialog);
		// TODO 自动生成的构造函数存根
		setCustomDialog();
		Window window = getWindow();
		/* 设置窗口弹出动画和消失动画 */
		window.setWindowAnimations(R.style.noWindowAnim);
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;
		lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
		lp.gravity = Gravity.CENTER;
		window.setAttributes(lp);
		setOnKeyListener(onKeyListener);
		setCanceledOnTouchOutside(true);
		mCancelable = true;
	}

	public synchronized static GeneralDialog create(Context context, String hint) {
		type = LOAD_HINT;
		if (generalDialog != null) {
			generalDialog.dismiss();
		}
		generalDialog = new GeneralDialog(context);
		generalDialog.setContent(hint);
		return generalDialog;
	}

	public synchronized static GeneralDialog create(Context context,
                                                    String hint, int displayType) {
		type = displayType;
		if (generalDialog != null) {
			generalDialog.dismiss();
		}
		generalDialog = new GeneralDialog(context);
		generalDialog.setContent(hint);
		return generalDialog;
	}

	/** 设置点击其他区域是否消失 */
	public GeneralDialog Cancelable(boolean cancelable) {
		generalDialog.setCanceledOnTouchOutside(cancelable);
		mCancelable = cancelable;
		return generalDialog;
	}

	/** 设置Dialog隐藏监听，方便隐藏时停止请求数据 */
	public GeneralDialog setDismissListener(final OnStopLoadListener listener) {
		generalDialog.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {
				// TODO 自动生成的方法存根
				listener.stopLoad();
			}
		});
		return generalDialog;
	}

	public void show() {
		if (content.getText().toString().equals("")) {
//			Logger.d("");
			Logger.d("提示文字不能为空");
		} else {
			try {
				super.show();
				if (mCancelable) {
					new Handler().postDelayed(new Runnable() {
						@Override
						public void run() {
							dismiss();
						}
					}, 1500);
				}
			} catch (Exception e) {
				// TODO: handle exception
			}

		}

	}

	private void setCustomDialog() {
		mView = LayoutInflater.from(getContext()).inflate(
				R.layout.dialog_general, null);
		progressBar = (ProgressBar) mView.findViewById(R.id.progressBar);
		resultIcon = (ImageView) mView.findViewById(R.id.resultIcon);
		switch (type) {
			case LOAD_HINT:
				resultIcon.setVisibility(View.GONE);
				progressBar.setVisibility(View.VISIBLE);
				break;
			case FAIL_HINT:
				progressBar.setVisibility(View.GONE);
				resultIcon.setBackgroundResource(R.drawable.warning_icon);
				resultIcon.setVisibility(View.VISIBLE);
				break;
			case SUCCESS_HINT:
				progressBar.setVisibility(View.GONE);
				resultIcon.setBackgroundResource(R.drawable.success_icon);
				resultIcon.setVisibility(View.VISIBLE);
				break;
		}
		content = (TextView) mView.findViewById(R.id.content);
		super.setContentView(mView);
	}

	public void disPlayProgress(int progress) {
		content.setText(progress + "%");
	}

	public void setContent(String str) {
		content.setText(str);
	}

	@Override
	public void setContentView(int layoutResID) {
	}

	@Override
	public void setContentView(View view, LayoutParams params) {
	}

	@Override
	public void setContentView(View view) {
	}

	public interface OnStopLoadListener {
		void stopLoad();
	}
}
